//
//  GQHContractLoadingInfoView.m
//  Xanthium
//
//  Created by GuanQinghao on 16/04/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHContractLoadingInfoView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"


@interface GQHContractLoadingInfoView () <UITableViewDelegate,UITableViewDataSource, UITextFieldDelegate>
{
    NSArray *rowTitleArray;
    NSArray *sectionTitleArray;
}
@property (nonatomic, strong) NSMutableArray * placeHolderArray;

@end


@implementation GQHContractLoadingInfoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    sectionTitleArray = @[@"装货信息",@"收货信息",@"业务类型及运输方式"];
    rowTitleArray = @[@"装货联系人",@"装货电话",@"装货地址",@"装货时间",@"收货联系人",@"收货电话",@"收货地址",@"业务类型",@"运输方式",@"车长",@"车辆类型"];
    _qh_contentArray = [NSMutableArray arrayWithArray:@[@"请输入",@"请输入",@"请输入",@"请选择",@"请输入",@"非必填",@"请输入",@"请选择",@"请选择",@"请选择",@"请选择"]];
    self.placeHolderArray = [NSMutableArray arrayWithObjects:@"",@"",@"",@"",@"请输入",@"非必填",@"",@"",@"",@"",@"", nil];
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return sectionTitleArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
            
        case 0:
            return 4;
            break;
            
        case 1:
            return 3;
            break;
            
        case 2:
            return 4;
            break;
            
        default:
            return 0;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    YYEnterTextCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"YYEnterTextCell" owner:self options:nil].firstObject;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.perLabel_width.constant = 0;
    switch (indexPath.section) {
            
        case 0: {
            cell.titleLabel.text = rowTitleArray[indexPath.row];
            cell.showValueLabel.text = _qh_contentArray[indexPath.row];
            cell.isEnter = NO;
        }
            break;
        case 1: {
            if (indexPath.row == 0 || indexPath.row == 1)
            {
                cell.isEnter = YES;
                cell.enterText.text = _qh_contentArray[indexPath.row + 4];
                cell.enterText.placeholder = self.placeHolderArray[indexPath.row + 4];
                cell.enterText.tag = indexPath.row + 10;
                cell.enterText.delegate = self;
            }
            else
            {
                cell.showValueLabel.text = _qh_contentArray[indexPath.row + 4];
                cell.isEnter = NO;
            }
            
            cell.titleLabel.text = rowTitleArray[indexPath.row + 4];
        }
            break;
        case 2: {
            cell.isEnter = NO;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.showValueLabel.text = _qh_contentArray[indexPath.row + 7];
            cell.titleLabel.text = rowTitleArray[indexPath.row + 7];
        }
            break;
            
        default:
            
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 30.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kLightGrayColor;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = sectionTitleArray[section];
    titleLabel.textColor = kAppRedColor;
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.mas_equalTo(headerView);
        make.left.and.right.mas_equalTo(headerView).with.inset(GQH_MARGIN_SINGLE);
    }];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

#pragma mark ---- UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [_qh_contentArray replaceObjectAtIndex:textField.tag - 10 + 4 withObject:textField.text];
}

@end
